// Magic Software, Inc.
// http://www.magic-software.com
// Copyright (c) 2000, All Rights Reserved
//
// Source code from Magic Software is supplied under the terms of a license
// agreement and may not be copied or disclosed except in accordance with the
// terms of that agreement.  The various license agreements may be found at
// the Magic Software web site.  This file is subject to the license
//
// RESTRICTED USE SOURCE CODE
// http://www.magic-software.com/License/restricted.pdf

#ifndef MGCBEZIERTRIANGLE3_H
#define MGCBEZIERTRIANGLE3_H

#include "MgcBezierTriangle.h"
#include "MgcColor.h"
#include "MgcVector2.h"
#include "MgcVector3.h"


class MgcBezierTriangle3 : public MgcBezierTriangle
{
    MgcDeclareRTTI;
    MgcDeclareStream;

public:
    // Construction.  MgcBezierTriangle3 accepts responsibility for deleting
    // the input array.
    MgcBezierTriangle3 (unsigned int* auiIndex);

    // tessellation
    virtual void Tessellate (unsigned int uiLevel,
        const MgcVector3* akCtrlPoint, const MgcColor* akCtrlColor,
        const MgcVector2* akCtrlTexture, MgcTriMesh* pkMesh,
        unsigned int& ruiVertexStart, unsigned int& ruiTriangleStart);

protected:
    MgcBezierTriangle3 ();

    // precomputation
    class BlockParameters
    {
    public:
        unsigned int m_uiI0, m_uiI1, m_uiI2;
        MgcVector3 m_aakXuu[3], m_aakXvv[3], m_aakXhh[3], m_kXuuu, m_kXuuv,
            m_kXuvv, m_kXvvv, m_kXhhu, m_kXhhv;
        MgcColor m_aakCuu[3], m_aakCvv[3], m_aakChh[3];
        MgcVector2 m_aakTuu[3], m_aakTvv[3], m_aakThh[3];
    };

    void InitializePoints (const MgcVector3* akCtrlPoint,
        BlockParameters& rkBP);

    void InitializeNormals (const MgcVector3* akCtrlPoint,
        BlockParameters& rkBP);

    void InitializeColors (const MgcColor* akCtrlColor,
        BlockParameters& rkBP);

    void InitializeTextures (const MgcVector2* akCtrlTexture,
        BlockParameters& rkBP);

    // tessellation by recursive subdivision

    // subdivide triangle with right angle at Lower Left corner
    void SubdivideLL (unsigned int uiLevel, MgcReal fDSqr, MgcVector3* akX,
        MgcVector3* akXu, MgcVector3* akXv, MgcColor* akColor,
        MgcVector2* akTexture, BlockParameters& rkBP);

    // subdivide triangle with right angle at Upper Right corner
    void SubdivideUR (unsigned int uiLevel, MgcReal fDSqr, MgcVector3* akX,
        MgcVector3* akXu, MgcVector3* akXv, MgcColor* akColor,
        MgcVector2* akTexture, BlockParameters& rkBP);
};

MgcSmartPointer(MgcBezierTriangle3);
MgcRegisterStream(MgcBezierTriangle3);

#endif
